<template>
	<div class="system-stocklog-container standard-container layout-padding layout-pd">
		<div class="review-list-title-end">
			<div class="title-actions">
				<el-button :icon="Refresh" @click="refresh"></el-button>
				<el-button :icon="Search" @click="onOpenSearch"></el-button>
			</div>
		</div>
		<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
			<el-table-column label="商品信息" width="500">
				<template #default="scope">
					<div class="flex goods-item">
						<div style="width: 80px; height: 80px">
							<el-image :src="STATIC_URL + scope.row.image" />
						</div>
						<div class="right ml15">
							<div class="goods-title">
								{{ scope.row.goodsName }}
							</div>
							<div class="goods-sku-text">
								<span v-for="(sku, index) in scope.row.skus" :key="sku" :class="{ ml5: index != 0 }"> {{ sku }} </span>
							</div>
						</div>
					</div>
				</template>
			</el-table-column>

			<el-table-column prop="stockHistory" label="补货前库存" show-overflow-tooltip></el-table-column>
			<el-table-column prop="stockNow" label="补充库存" show-overflow-tooltip></el-table-column>
			<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
			<el-table-column prop="createUsername" label="操作人" show-overflow-tooltip width="200"></el-table-column>
		</el-table>
		<div class="pagination-container">
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				:page-sizes="[10, 15, 20, 30, 50, 100]"
				v-model:current-page="state.tableData.paging.currentPage"
				background
				v-model:page-size="state.tableData.paging.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.paging.totalCount"
			>
			</el-pagination>
		</div>

		<SearchDialog ref="SearchDialogRef"></SearchDialog>
	</div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue';
import { goodsStockLogApi } from '/@/api/goods/stocklog';
import msg from '/@/utils/messageUtils';
import { STATIC_URL } from '/@/utils/config';
import { Refresh, Search } from '@element-plus/icons-vue';

const SearchDialog = defineAsyncComponent(() => import('./searchDialog.vue'));
const SearchDialogRef = ref();
const state = reactive({
	isToggle: false,
	search: {
		goodsName: '',
		stockWarningStatus: '',
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		paging: {
			currentPage: 1,
			pageSize: 15,
			totalCount: 0,
			totalPages: 0,
		},
	},
});

const onHandleSizeChange = (pageSize: number) => {
	state.tableData.paging.pageSize = pageSize;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
	state.tableData.paging.currentPage = currentPage;
	getTableData();
};
const getTableData = async () => {
	try {
		const { data } = await goodsStockLogApi().search({
			offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
			limit: state.tableData.paging.pageSize,
			orderStatus: state.search.stockWarningStatus,
		});

		console.log('result: ', data);
		state.tableData.data = data.datas;
		state.tableData.paging.currentPage = data.paging.currentPage;
		state.tableData.paging.totalCount = data.paging.totalCount;
	} catch (e) {
		msg().error('查询失败，请稍后重试');
	}
};

const refresh = () => {
	onHandleCurrentChange(1);
};

const onOpenSearch = () => {
	SearchDialogRef.value.handleOpen();
};

onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-stocklog-container {
	.el-table {
		flex: 1;
	}
}
</style>
